package com.mdlib.helper.module.account.fragments;

import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.core.util.common.StringUtils;
import com.ljlib.core.widget.CountdownButton;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.interfaces.ITextChangedListener;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.utils.MDAppUtils;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by librabin on 16/11/24.
 */

public class BindPhoneFragment extends BaseTitleFragment {

    @Bind(R.id.et_bind_phone)
    EditText mEtBindPhone;
    @Bind(R.id.tv_bind_code)
    CountdownButton mTvBindCode;
    @Bind(R.id.et_bind_code)
    EditText mEtBindCode;
    @Bind(R.id.tv_bind_next)
    TextView mTvBindNext;

    public static BindPhoneFragment newInstance() {
        return new BindPhoneFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_bind_phone;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);

        setTitle(R.string.bind_tv_title, R.color.white)
                .setTitleBgColor(R.color.myred)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });


        mEtBindPhone.addTextChangedListener(new ITextChangedListener() {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11 && !mTvBindCode.isRunning()) {
                    mTvBindCode.setEnabled(true);
                } else {
                    mTvBindCode.setEnabled(false);
                }
            }
        });

    }


    @OnClick({R.id.tv_bind_code, R.id.tv_bind_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_bind_code:
                String phoneNum1 = mEtBindPhone.getText().toString();
                if (!MDAppUtils.checkPhoneNum(phoneNum1)) {
                    return;
                }
                featchCode(phoneNum1, "login");
                break;
            case R.id.tv_bind_next:
                String phone = mEtBindPhone.getText().toString();
                String code = mEtBindCode.getText().toString();
                if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code)) {
                    ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_not_empty));
                    return;
                }
                addFragment(SetPwdFragment.newInstance(phone, code));
                break;
        }
    }

    /**
     * 获取验证码
     *
     * @param phoneNum
     * @param codeType
     */
    private void featchCode(String phoneNum, String codeType) {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .featchPhoneCode(phoneNum, codeType)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {

                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));

    }
}
